/**
  ************************************* Copyright ******************************
  * FileName   : screen.c
  * Version    : v1.0
  * Author     : yychao
  * Date       : 2023-06-16
  * Description:
  * Function List:
  	1. ....
  	   <version>:
  <modify staff>:
  		  <data>:
   <description>:
  	2. ...
  ******************************************************************************
 */

#include <rtthread.h>
#include "screen.h"
#include "usart.h"
#include "stddef.h"
#include "string.h"
#include "log.h"
#include "rtc.h"
#include "lfs_port.h"
#include "io_ctrl.h"
const uint8_t chk_array[6]= {0x5a, 0xa5, 0x03, 0x82, 0x4f, 0x4b};

extern struct rt_messagequeue static_mq1;
extern struct rt_mutex static_mutex0;
extern struct rt_semaphore static_sem1;
struct protocol screen_prot;
struct screen_para screen_para_st;
struct screen_time screen_time_st;
struct screen_curve screen_curve_st;
struct screen_bakectrl screen_bakectrl_st;
void screen_deal(usart_cache* recv)
{
    screen_adv(&recv->data[3], recv->cnt - 3);
}

void screen_adv(uint8_t *data, uint16_t len)
{
    uint8_t cache[64] = { 0x00 };
    if ((len - 3) != data[2]) {
        for (uint16_t j = 0; j < len; j++) {
            LOG_ERROR("%x ", data[j]);
        }
        LOG_ERROR("\r\n");
        LOG_ERROR("Stick the data package!\r\n");
        for (uint16_t i = 0; i < len;) {
            if ((data[i] == 0x5A) && (data[i + 1] == 0xA5)) {
                memset(&cache[0], 0x00, 64);
                memcpy(&cache[0], &data[i], data[i + 2] + 3);
                screen_frame(&cache[0], data[i + 2] + 3);
                i = i + data[i + 2] + 3;
            } else {
                i++;
            }
        }
    } else {
        screen_frame(&data[0], len);
    }
}

void screen_frame(uint8_t *data, uint16_t len)
{
    uint8_t type = 0;
    if(screen_para_st.wanter_flag == 1) {
        if(0 == memcmp(&data[0], &screen_para_st.wanter_data[0], len)) {
            rt_sem_release(&static_sem1);
            screen_para_st.wanter_flag = 0;
            type = 1;
        } else {
            /**/
            type = 2;
        }
    } else {
        rt_mq_send(&static_mq1, &data[0], SCREEN_MQ_BUF_SIZE);
        type = 3;
        for (uint16_t i = 0; i < len; i++) {
            LOG_DEBUG("%02x ", data[i]);
        }
        LOG_DEBUG("[RxLen %d Type %d] \r\n", len, type);
    }
}


void Screen_OutQueue(void)
{
    uint8_t buf[SCREEN_MQ_BUF_SIZE];
    uint16_t cnt = 0;
    if(0 == rt_mq_recv(&static_mq1,&buf[0], sizeof(buf), 0)) {
        screen_touch(&buf[0],128);
    }
}
uint8_t screen_touch(uint8_t *data, uint16_t len)
{

    uint16_t maxdate = 0;
    uint16_t temp = 0;

    screen_prot.head = ((uint16_t) data[0] << 8) | (data[1]);
    screen_prot.len = data[2];
    screen_prot.cmd = data[3];
#ifdef SCREEN_CRC
    screen_prot.crc = ((uint16_t) data[len - 2] << 8) | (data[len - 1]);
#endif
    screen_prot.addr = ((uint16_t) data[4] << 8) | (data[5]);
    screen_prot.byte = data[6];
    switch(screen_prot.byte) {
    case 1:
        screen_prot.value_16 = ((uint16_t) data[7] << 8) | (data[8]);
        break;
    case 2:
        screen_prot.value_32 = ((uint32_t) data[7] << 8 * 3) | (data[8] << 8 * 2)
                               | (data[9] << 8) | (data[10]);
        break;
    default:
        NULL;
    }
    if (PROT_HEAD != screen_prot.head) {
        screen_prot.err_code = head_err;
        goto err_ret;
    }
#if 0
    if ((len - 3) != screen_prot.len) {
        screen_prot.err_code = len_err;
        goto err_ret;
    }
#endif
    //#define SCREEN_CRC
#ifdef SCREEN_CRC
    if(CRC16_Modbus(&data[0], len - 2) != screen_prot.crc) {
        screen_prot.err_code = crc_err;
        goto err_ret;
    }
#endif
    switch (screen_prot.cmd) {
    case Touch:
        switch(screen_prot.addr) {
        /*page main*/
        case T_MAIN_2_MENU:
            screen_page(P_MENU);
            break;
        case T_MAIN_2_CTRL:
            lfs_user("bake_ctrl", &bake_ctrl[0], Len_bake_ctrl, Lfs_Read);
            memcpy(&screen_bakectrl_st.value[0], &bake_ctrl[0], sizeof(uint16_t)*10);
            screen_write(TD_CTRL_BFAN, (uint16_t*)&screen_bakectrl_st.value[0], 4);
            screen_page(P_CTRL);
            break;
        /*page menu*/
        case T_MENU_2_MAIN:
            screen_page(P_MAIN);
            break;
        case T_MENU_2_TIME:
            RTC_GetDate(RTC_24HOUR_FORMAT, &RTC_DateStructure);
            screen_time_st.Year = RTC_DateStructure.Year ;
            screen_time_st.Month  = RTC_DateStructure.Month;
            screen_time_st.Date  =RTC_DateStructure.Date;
            screen_time_st.Week =RTC_DateStructure.WeekDay;
            temp = RTC_DateStructure.Year;
            screen_write(TD_TIME_YEAR, &temp, 1);
            temp = RTC_DateStructure.Month;
            screen_write(TD_TIME_MONTH, &temp, 1);
            temp = RTC_DateStructure.Date;
            screen_write(TD_TIME_DATE, &temp, 1);
            temp = RTC_DateStructure.WeekDay;
            screen_write(TD_TIME_WEEK, &temp, 1);

            RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
            screen_time_st.Hour =RTC_TimeStructure.Hours;
            screen_time_st.Minute =RTC_TimeStructure.Minutes;
            screen_time_st.Second  =RTC_TimeStructure.Seconds;
            temp = RTC_TimeStructure.Hours;
            screen_write(TD_TIME_HOUR, &temp, 1);
            temp = RTC_TimeStructure.Minutes;
            screen_write(TD_TIME_MINUTE, &temp, 1);
            temp = RTC_TimeStructure.Seconds;
            screen_write(TD_TIME_SECOND, &temp, 1);

            LOG_DEBUG("The current date(Week-Date-Month-Year)\r\n%0.2d-%0.2d-%0.2d-%0.2d\r\n",
                      RTC_DateStructure.WeekDay,
                      RTC_DateStructure.Date,
                      RTC_DateStructure.Month,
                      RTC_DateStructure.Year);
            LOG_DEBUG("The current time(Hour-Minute-Second)\r\n%0.2d:%0.2d:%0.2d\r\n",
                      RTC_TimeStructure.Hours,
                      RTC_TimeStructure.Minutes,
                      RTC_TimeStructure.Seconds);
            screen_page(P_TIME);
            break;
        case T_MENU_2_CURVE:
            lfs_user("curve", &curve[0], Len_curve, Lfs_Read);
            memcpy(&screen_curve_st.dry[0], &curve[0], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.wet[0], &curve[10], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.up[0], &curve[20], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.std[0], &curve[30], sizeof(uint16_t)*10);
            screen_write(TD_CURVE_DRY1, (uint16_t*)&screen_curve_st.dry[0], 10);
            screen_write(TD_CURVE_WET1, (uint16_t*)&screen_curve_st.wet[0], 10);
            screen_write(TD_CURVE_UP1, (uint16_t*)&screen_curve_st.up[0], 10);
            screen_write(TD_CURVE_STD1, (uint16_t*)&screen_curve_st.std[0], 10);
            screen_page(P_CURVE);
            break;
        case T_MENU_2_BK:
            screen_page(P_BURN);
            break;
        /*page time*/
        case T_TIME_2_MENU:

            screen_page(P_MENU);
            break;
        case T_TIME_SAVE:

            maxdate = MaxDate((screen_time_st.Year), screen_time_st.Month);
            if(screen_time_st.Date > maxdate) {
                screen_time_st.Date = maxdate;
            }
            LOG_DEBUG("%d-%d-%d %d:%d:%d %d\r\n",screen_time_st.Year,
                      screen_time_st.Month,
                      screen_time_st.Date,
                      screen_time_st.Hour,
                      screen_time_st.Minute,
                      screen_time_st.Second,
                      screen_time_st.Week);

            RTC_DateStructure.Year = screen_time_st.Year;
            RTC_DateStructure.Month = screen_time_st.Month;
            RTC_DateStructure.Date = screen_time_st.Date;
            RTC_DateStructure.WeekDay = screen_time_st.Week;
            RTC_TimeStructure.Hours = screen_time_st.Hour;
            RTC_TimeStructure.Minutes = screen_time_st.Minute;
            RTC_TimeStructure.Seconds = screen_time_st.Second;
            /* Configure the RTC date register */
            if (RTC_SetDate(RTC_FORMAT_BIN, &RTC_DateStructure) == ERROR)
            {
                LOG_DEBUG("RTC Set Date failed...\r\n");
            }
            /* Configure the RTC time register */
            if (RTC_ConfigTime(RTC_FORMAT_BIN, &RTC_TimeStructure) == ERROR)
            {
                LOG_DEBUG("RTC Set Time failed...\r\n");
            }
            RTC_GetDate(RTC_24HOUR_FORMAT, &RTC_DateStructure);
            RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
            LOG_DEBUG("The current date(Week-Date-Month-Year)\r\n%0.2d-%0.2d-%0.2d-%0.2d\r\n",
                      RTC_DateStructure.WeekDay,
                      RTC_DateStructure.Date,
                      RTC_DateStructure.Month,
                      RTC_DateStructure.Year);
            LOG_DEBUG("The current time(Hour-Minute-Second)\r\n%0.2d:%0.2d:%0.2d\r\n",
                      RTC_TimeStructure.Hours,
                      RTC_TimeStructure.Minutes,
                      RTC_TimeStructure.Seconds);
            temp = RTC_DateStructure.Date;
            screen_write(TD_TIME_DATE, &temp, 1);
            break;
        case TD_TIME_YEAR:
            screen_time_st.Year = screen_prot.value_16;
            break;
        case TD_TIME_MONTH:
            screen_time_st.Month = screen_prot.value_16;
            break;
        case TD_TIME_DATE:
            screen_time_st.Date = screen_prot.value_16;
            break;
        case TD_TIME_HOUR:
            screen_time_st.Hour = screen_prot.value_16;
            break;
        case TD_TIME_MINUTE:
            screen_time_st.Minute = screen_prot.value_16;
            break;
        case TD_TIME_SECOND:
            screen_time_st.Second = screen_prot.value_16;
            break;
        case TD_TIME_WEEK:
            screen_time_st.Week = screen_prot.value_16;
            break;
        /*page curve*/
        case T_CURVE_2_MENU:
            lfs_user("curve", &curve[0], Len_curve, Lfs_Read);
            memcpy(&screen_curve_st.dry[0], &curve[0], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.wet[0], &curve[10], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.up[0], &curve[20], sizeof(uint16_t)*10);
            memcpy(&screen_curve_st.std[0], &curve[30], sizeof(uint16_t)*10);
            screen_write(TD_CURVE_DRY1, (uint16_t*)&screen_curve_st.dry[0], 10);
            screen_write(TD_CURVE_WET1, (uint16_t*)&screen_curve_st.wet[0], 10);
            screen_write(TD_CURVE_UP1, (uint16_t*)&screen_curve_st.up[0], 10);
            screen_write(TD_CURVE_STD1, (uint16_t*)&screen_curve_st.std[0], 10);
            if((ctrl_state_st.stage_item % 2) == 0) {
                ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
                ctrl_state_st.final_dry_aim /=10;
                ctrl_state_st.all_time = screen_curve_st.up[ctrl_state_st.stage];
                ctrl_state_st.all_time /= 10;
//                    ctrl_state_st.past_time = 0;
                ctrl_state_st.residual_time = ctrl_state_st.all_time - ctrl_state_st.past_time;
            } else {
                ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
                ctrl_state_st.final_dry_aim /=10;
                ctrl_state_st.all_time = screen_curve_st.std[ctrl_state_st.stage];
                ctrl_state_st.all_time /= 10;
//                    ctrl_state_st.past_time = 0;
                ctrl_state_st.residual_time = ctrl_state_st.all_time - ctrl_state_st.past_time;
            }

            temp = screen_curve_st.dry[ctrl_state_st.stage];
            screen_write(DIS_MAIN_AIM_DRY, (uint16_t*)&temp, 1);

            screen_page(P_MENU);
            break;
        case T_CURVE_SAVE:
            memcpy(&curve[0], &screen_curve_st.dry[0], sizeof(uint16_t)*10);
            memcpy(&curve[10], &screen_curve_st.wet[0], sizeof(uint16_t)*10);
            memcpy(&curve[20], &screen_curve_st.up[0], sizeof(uint16_t)*10);
            memcpy(&curve[30], &screen_curve_st.std[0], sizeof(uint16_t)*10);
            lfs_user("curve", &curve[0], Len_curve, Lfs_Write);
            break;
        case TD_CURVE_DRY1:
        case TD_CURVE_DRY2:
        case TD_CURVE_DRY3:
        case TD_CURVE_DRY4:
        case TD_CURVE_DRY5:
        case TD_CURVE_DRY6:
        case TD_CURVE_DRY7:
        case TD_CURVE_DRY8:
        case TD_CURVE_DRY9:
        case TD_CURVE_DRY10:
            for(uint8_t i = 0 ; i < 10; i++) {
                if(screen_prot.addr == (TD_CURVE_DRY1 + i)) {
                    screen_curve_st.dry[i] = screen_prot.value_16;
                }
            }
            break;
        case TD_CURVE_WET1:
        case TD_CURVE_WET2:
        case TD_CURVE_WET3:
        case TD_CURVE_WET4:
        case TD_CURVE_WET5:
        case TD_CURVE_WET6:
        case TD_CURVE_WET7:
        case TD_CURVE_WET8:
        case TD_CURVE_WET9:
        case TD_CURVE_WET10:
            for(uint8_t i = 0 ; i < 10; i++) {
                if(screen_prot.addr == (TD_CURVE_WET1 + i)) {
                    screen_curve_st.wet[i] = screen_prot.value_16;
                }
            }
            break;
        case TD_CURVE_UP1:
        case TD_CURVE_UP2:
        case TD_CURVE_UP3:
        case TD_CURVE_UP4:
        case TD_CURVE_UP5:
        case TD_CURVE_UP6:
        case TD_CURVE_UP7:
        case TD_CURVE_UP8:
        case TD_CURVE_UP9:
        case TD_CURVE_UP10:
            for(uint8_t i = 0 ; i < 10; i++) {
                if(screen_prot.addr == (TD_CURVE_UP1 + i)) {
                    screen_curve_st.up[i] = screen_prot.value_16;
                }
            }
            break;
        case TD_CURVE_STD1:
        case TD_CURVE_STD2:
        case TD_CURVE_STD3:
        case TD_CURVE_STD4:
        case TD_CURVE_STD5:
        case TD_CURVE_STD6:
        case TD_CURVE_STD7:
        case TD_CURVE_STD8:
        case TD_CURVE_STD9:
        case TD_CURVE_STD10:
            for(uint8_t i = 0 ; i < 10; i++) {
                if(screen_prot.addr == (TD_CURVE_STD1 + i)) {
                    screen_curve_st.std[i] = screen_prot.value_16;
                }
            }
            break;
        /*page bk*/
        case T_BK_2_MENU:
            screen_page(P_MENU);
            break;
        case T_BK_SAVE:
            break;
        /*page ctrl*/
        case T_CTRL_2_MAIN:
            screen_page(P_MAIN);
            break;
        case T_CTRL_SAVE1:
            memcpy(&bake_ctrl[0], &screen_bakectrl_st.value[0], sizeof(uint16_t)*10);
            lfs_user("bake_ctrl", &bake_ctrl[0], Len_bake_ctrl, Lfs_Write);
            break;
        case T_CTRL_BAKE:
            if(ctrl_state_st.status == 0) {
                temp = PIC_ON;
                screen_write(P_CTRL_BAKE, (uint16_t*)&temp, 1);

                ctrl_state_st.status = 1;
                ctrl_state_st.time_sta = 1;
                ctrl_state_st.time_cnt = 0;
                ctrl_state_st.time_chk = 0;

                ctrl_state_st.feed_start = 1;
                ctrl_state_st.feed_cnt = 0;
                if(dev_sta_real.feed == 0) {
                    dev_sta_ready.feed = 1;
                }

                ctrl_state_st.clear_start = 1;
                ctrl_state_st.clear_cnt = 0;
                if(dev_sta_real.clear == 0) {
                    dev_sta_ready.clear = 1;
                }


                if((ctrl_state_st.stage_item % 2) == 0) {
                    ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
                    ctrl_state_st.final_dry_aim /=10;
                    ctrl_state_st.all_time = screen_curve_st.up[ctrl_state_st.stage];
                    ctrl_state_st.all_time /= 10;
//                    ctrl_state_st.past_time = 0;
                    ctrl_state_st.residual_time = ctrl_state_st.all_time - ctrl_state_st.past_time;
                } else {
                    ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
                    ctrl_state_st.final_dry_aim /=10;
                    ctrl_state_st.all_time = screen_curve_st.std[ctrl_state_st.stage];
                    ctrl_state_st.all_time /= 10;
//                    ctrl_state_st.past_time = 0;
                    ctrl_state_st.residual_time = ctrl_state_st.all_time - ctrl_state_st.past_time;
                }

                temp = screen_curve_st.dry[ctrl_state_st.stage];
                screen_write(DIS_MAIN_AIM_DRY, (uint16_t*)&temp, 1);
            } else if(ctrl_state_st.status) {
                temp = PIC_OFF;
                screen_write(P_CTRL_BAKE, (uint16_t*)&temp, 1);

                if(dev_sta_real.fire != 0) {
                    dev_sta_ready.fire = 0;
                }
                if(dev_sta_real.feed != 0) {
                    dev_sta_ready.feed = 0;
                    dev_sta_real.feed = 0;
                    gpio_off(GPIOB, GPIO_PIN_12);
                    temp = PIC_OFF;
                    screen_write(P_CTRL_FEED, (uint16_t*)&temp, 1);
                }
                if(dev_sta_real.clear != 0) {
                    dev_sta_ready.clear = 0;
                    dev_sta_real.clear = 0;
                    gpio_off(GPIOE, GPIO_PIN_12);
                    temp = PIC_OFF;
                    screen_write(P_CTRL_CLEAR, (uint16_t*)&temp, 1);
                }

                if(dev_sta_real.burn_fan != 0) {
                    dev_sta_ready.burn_fan = 0;
                }
                if(dev_sta_real.cycle_fan != 0) {
                    dev_sta_ready.cycle_fan = 0;
                }
                if(dev_sta_real.air_door != 0) {
                    dev_sta_ready.air_door = 0;
                }
                ctrl_state_st.status = 0;
                ctrl_state_st.time_sta = 0;
                ctrl_state_st.time_cnt = 0;
                ctrl_state_st.time_chk = 0;
            }
            break;
        case T_CTRL_FIRE:
            if(dev_sta_real.fire == 0) {
                dev_sta_ready.fire = 1;
            } else {
                dev_sta_ready.fire = 0;
            }
            break;
        case T_CTRL_FEED:
            if(ctrl_state_st.status == 0) {
                if(dev_sta_real.feed == 0) {
                    dev_sta_ready.feed = 1;
                } else {
                    dev_sta_ready.feed = 0;
                }
            }
            break;
        case T_CTRL_BFAN:
            if(ctrl_state_st.status == 0) {
                if(dev_sta_real.burn_fan == 0) {
                    dev_sta_ready.burn_fan = 1;
                } else {
                    dev_sta_ready.burn_fan = 0;
                }
            }
            break;
        case T_CTRL_CYCLE:
            if(ctrl_state_st.status == 0) {
                if(dev_sta_real.cycle_fan == 0) {
                    dev_sta_ready.cycle_fan = 1;
                } else {
                    dev_sta_ready.cycle_fan = 0;
                }
            }
            break;
        case T_CTRL_AIR:
            if(ctrl_state_st.status == 0) {
                if(dev_sta_real.air_door == 0) {
                    dev_sta_ready.air_door = 1;
                } else {
                    dev_sta_ready.air_door = 0;
                }
            }
            break;
        case T_CTRL_CLEAR:
            if(ctrl_state_st.status == 0) {
                if(dev_sta_real.clear == 0) {
                    dev_sta_ready.clear = 1;
                } else if(dev_sta_real.clear == 1){
                    dev_sta_ready.clear = 2;
                } else if(dev_sta_real.clear == 2){
					dev_sta_ready.clear = 0;
				}
            }
            break;
        case T_CTRL_INI:

            if(ctrl_state_st.status == 0) {
                temp = PIC_ON;
                screen_write(P_CTRL_BAKE, (uint16_t*)&temp, 1);

                ctrl_state_st.status = 1;
                ctrl_state_st.stage = 0;
                ctrl_state_st.stage_item = 0;
                ctrl_state_st.time_sta = 1;
                ctrl_state_st.time_cnt = 0;
                ctrl_state_st.time_chk = 0;

                ctrl_state_st.feed_start = 1;
                ctrl_state_st.feed_cnt = 0;
                if(dev_sta_real.feed == 0) {
                    dev_sta_ready.feed = 1;
                }
                ctrl_state_st.clear_start = 1;
                ctrl_state_st.clear_cnt = 0;
                if(dev_sta_real.clear == 0) {
                    dev_sta_ready.clear = 1;
                }
                ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
                ctrl_state_st.final_dry_aim /=10;
                ctrl_state_st.all_time = screen_curve_st.up[ctrl_state_st.stage];
                ctrl_state_st.all_time /= 10;
                ctrl_state_st.past_time = 0;
                //ctrl_state_st.residual_time = ctrl_state_st.all_time - ctrl_state_st.past_time;
                ctrl_state_st.residual_time = ctrl_state_st.all_time;
                temp = ctrl_state_st.stage_item + 1;
                screen_write(DIS_MAIN_STAGE, (uint16_t*)&temp, 1);
                temp = screen_curve_st.dry[ctrl_state_st.stage];
                screen_write(DIS_MAIN_AIM_DRY, (uint16_t*)&temp, 1);
            }
            break;
        case TD_CTRL_BFAN:
            screen_bakectrl_st.value[0] = screen_prot.value_16;
            break;
        case TD_CTRL_CYCLE:
            screen_bakectrl_st.value[1] = screen_prot.value_16;
            break;
        case TD_CTRL_AIR:
            screen_bakectrl_st.value[2] = screen_prot.value_16;
            break;
        case TD_CTRL_CLEAR:
            screen_bakectrl_st.value[3] = screen_prot.value_16;
            break;
        default:
            NULL;
        }
        break;
    case Write:
        break;
    default:
        screen_prot.err_code = cmd_err;
        goto err_ret;
    }
    return OK;
err_ret:
    LOG_ERROR("Format error: err-code %d \r\n", screen_prot.err_code);
    return FAILED;
}
uint8_t screen_write(uint16_t addr, uint16_t *value, uint16_t num)
{
    if( 0 != rt_mutex_take(&static_mutex0, RT_WAITING_FOREVER)) {
        LOG_ERROR("rt_mutex_take static_mutex0 Failed!\r\n");
        rt_mutex_release(&static_mutex0);
        return FAILED;
    }
    memset(&screen_para_st.trans_data[0], 0x00, 128);
    screen_para_st.trans_data[0] = PROT_HEAD >> 8;
    screen_para_st.trans_data[1] = (uint8_t) PROT_HEAD;
    screen_para_st.trans_data[2] = 0x03 + num * 2;
    screen_para_st.trans_data[3] = Write;
    screen_para_st.trans_data[4] = addr >> 8;
    screen_para_st.trans_data[5] = (uint8_t) addr;
    for (uint8_t i = 0; i < num; i++) {
        screen_para_st.trans_data[6 + i * 2] = value[0 + i] >> 8;
        screen_para_st.trans_data[7 + i * 2] = (uint8_t) value[0 + i];
    }
    screen_para_st.trans_len = 6 + num * 2;
    screen_para_st.wanter_flag = 1;
    screen_para_st.ms = 300;
    screen_para_st.wanter_data[0] = 0x00;
    memcpy(&screen_para_st.wanter_data[0], &chk_array[0], sizeof(chk_array));
    if(0 != screen_data_check(&screen_para_st)) {
        LOG_ERROR("Screen Check receive Failed!\r\n");
    }
    rt_mutex_release(&static_mutex0);
    return OK;
}

uint8_t screen_data_check(struct screen_para* st)
{
    uint8_t ret = 0;
    usart_trans(UART4, &st->trans_data[0], st->trans_len);
#if 1
    for (uint16_t i = 0; i < st->trans_len; i++) {
        LOG_DEBUG("%02x ", st->trans_data[i]);
    }
#endif
    LOG_DEBUG("[TxLen %d] \r\n", st->trans_len);
    if(0 == rt_sem_take(&static_sem1, st->ms))
    {
        ret = 0;
    } else {
        if(st->wanter_flag == 0) {
            ret = 0;
        } else {
            ret = 1;
        }
    }
    return ret;
}
uint8_t screen_page(uint16_t page)
{
    uint16_t cache[2] = { 0x5A01, 0xffff};
    cache[1] = page;
    if(0 != screen_write(0x0084,(uint16_t*)&cache[0], 2)) {
        return FAILED;
    } else {
        return OK;
    }
}
uint8_t screen_ini(void)
{
    uint16_t temp = 0;
#if 0
    while(0 != screen_page(P_CONNECT)) {
    }
    rt_thread_delay(1000);
#endif
    while(0 != 0 != screen_page(P_INITIAL)) {
    }
    rt_thread_delay(1000);
    lfs_user("curve", &curve[0], Len_curve, Lfs_Read);
    memcpy(&screen_curve_st.dry[0], &curve[0], sizeof(uint16_t)*10);
    memcpy(&screen_curve_st.wet[0], &curve[10], sizeof(uint16_t)*10);
    memcpy(&screen_curve_st.up[0], &curve[20], sizeof(uint16_t)*10);
    memcpy(&screen_curve_st.std[0], &curve[30], sizeof(uint16_t)*10);
    screen_write(TD_CURVE_DRY1, (uint16_t*)&screen_curve_st.dry[0], 10);
    screen_write(TD_CURVE_WET1, (uint16_t*)&screen_curve_st.wet[0], 10);
    screen_write(TD_CURVE_UP1, (uint16_t*)&screen_curve_st.up[0], 10);
    screen_write(TD_CURVE_STD1, (uint16_t*)&screen_curve_st.std[0], 10);


    lfs_user("bake_ctrl", &bake_ctrl[0], Len_bake_ctrl, Lfs_Read);
    memcpy(&screen_bakectrl_st.value[0], &bake_ctrl[0], sizeof(uint16_t)*10);
#if 1
    screen_write(TD_CTRL_BFAN, (uint16_t*)&screen_bakectrl_st.value[0], 4);
#else
    screen_write(TD_CTRL_BFAN, (uint16_t*)&screen_curve_st.dry[0], 1);
    screen_write(TD_CTRL_CYCLE, (uint16_t*)&screen_curve_st.dry[0], 2);
    screen_write(TD_CTRL_AIR, (uint16_t*)&screen_curve_st.dry[0], 3);
    screen_write(TD_CTRL_CLEAR, (uint16_t*)&screen_curve_st.dry[0], 4);
#endif
    temp = PIC_OFF;
    screen_write(P_CTRL_BAKE, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_FIRE, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_FEED, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_BFAN, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_CYCLE, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_AIR, (uint16_t*)&temp, 1);
    screen_write(P_CTRL_CLEAR, (uint16_t*)&temp, 1);


    ctrl_state_st.stage = 0;
    ctrl_state_st.stage_item = 0;
    ctrl_state_st.status = 0;
    ctrl_state_st.time_sta = 0;
    ctrl_state_st.time_cnt = 0;
    ctrl_state_st.past_time = 0;
    ctrl_state_st.residual_time = ctrl_state_st.all_time;
    temp = ctrl_state_st.stage_item + 1;
    screen_write(DIS_MAIN_STAGE, (uint16_t*)&temp, 1);
    temp = ctrl_state_st.past_time * 10;
    screen_write(DIS_MAIN_PAST, (uint16_t*)&temp, 1);
    ctrl_state_st.final_dry_aim = screen_curve_st.dry[ctrl_state_st.stage];
    temp = screen_curve_st.dry[ctrl_state_st.stage];
    screen_write(DIS_MAIN_AIM_DRY, (uint16_t*)&temp, 1);
    while(0 != screen_page(P_MAIN)) {
    }
    rt_thread_delay(1000);

#if 0
    rt_thread_delay(1000);
    page[0] = 0x5A01;
    page[1] = 0x000b;
//        screen_write(0x0084,(uint16_t*)&page[0], 2);
    rt_thread_delay(1000);
    page[0] = 0x5A01;
    page[1] = 0x000c;
//        screen_write(0x0084,(uint16_t*)&page[0], 2);
    rt_thread_delay(1000);
#endif
}
